package com.example.base.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.base.entity.Chapter;
import com.example.base.service.ChapterService;
import com.example.base.util.Response;
import com.example.base.util.validator.ChapterCtrlValidate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chapter")
public class ChapterController {

    @Autowired
    ChapterService chapterService;

    @GetMapping("/get")
    public Response getCourse(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size",required = false) Integer size,
            @RequestParam(value = "spId",required = true) String spId,
            @RequestParam(value = "courseName",required = false) String courseName,
            Chapter chapter){
        if (ObjectUtils.isEmpty(page)) {
            page = 1;
        }
        if (ObjectUtils.isEmpty(size)) {
            size = 5;
        }
        Response response = new Response();
        ChapterCtrlValidate.getChapterValidate(chapter, response);

        if (ObjectUtils.isNotEmpty(response.getError())){
            return response;
        }

        // 将""变成null，否则sql的!=null不成立
        if (chapter.getChapterName().equals("")){
            chapter.setChapterName(null);
        }

        if (chapter.getStatus().equals("")){
            chapter.setStatus(null);
        }

        Map<String, Object> map = chapterService.selectChapter(chapter, page, size, courseName);


        response.setData(map);
        response.setMsg("课程章节查询成功");
        response.setStatus(200);

        return response;
    }

    @PostMapping("/add")
    public Response addChapter(@RequestBody Chapter chapter){

        // 生成添加时间
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = simpleDateFormat.format(now);

        chapter.setCreateTime(createTime);

        Response response = new Response();

        int statusNum = chapterService.addChapter(chapter);

        if (statusNum == 1) {
            response.setStatus(200);
            response.setData(statusNum);
            response.setMsg("章节添加成功");
        }
        return response;
    }

    @PutMapping("/update")
    public Response updateChapter(@RequestBody Chapter chapter){

        // 生成添加修改时间
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String updateTime = simpleDateFormat.format(now);

        chapter.setUpdateTime(updateTime);

        Response response = new Response();

        int statusNum = chapterService.updateChapter(chapter);

        if (statusNum == 1){
            response.setStatus(200);
            response.setData(statusNum);
            response.setMsg("修改章节成功");
        }

        return response;
    }

    @PutMapping("/del")
    public Response delChapter(
            @RequestParam(value = "chapterId", required = true) String chapterId
    ){

        Response response = new Response();

        int statusNum = chapterService.delChapter(chapterId);

        response.setData(statusNum);
        if (statusNum == 1){
            response.setStatus(200);
            response.setMsg("执行删除命令成功");
        }else {
            response.setStatus(400);
            response.setMsg("执行删除命令成功，但没有此chapterId");
        }

        return response;
    }
}
